import os

def concatenate_txt_files_in_folder(folder_path, output_file):
    with open(output_file, 'w', encoding='utf-8') as outfile:
        # 遍历文件夹中的所有文件
        for file_name in os.listdir(folder_path):
            # 检查文件扩展名是否为 .txt
            if file_name.endswith('.txt'):
                file_path = os.path.join(folder_path, file_name)
                try:
                    with open(file_path, 'r', encoding='utf-8') as infile:
                        # 将文件内容写入输出文件
                        outfile.write(infile.read())
                except Exception as e:
                    print(f"处理文件 {file_path} 时出错：{e}")

if __name__ == '__main__':
    folder_path = 'tmp_txt/03_split/header'
    output_file = 'headers.txt'
    concatenate_txt_files_in_folder(folder_path, output_file)
    print(f"所有 .txt 文件已成功拼接到 {output_file}")